package com.labot.dto.mapper;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.springframework.dao.DataIntegrityViolationException;

import com.labot.dto.CentroDto;
import com.labot.dto.UserDto;
import com.labot.model.Centro;
import com.labot.model.Role;
import com.labot.model.User;


public class CentroMapper {
	
	public static CentroDto mapModelToDto(Centro source) {
		//Copiar las propiedades por Reflection
		CentroDto target = new CentroDto();
		try {
			BeanUtilsBean.getInstance().getConvertUtils().register(false, false, 0);
			BeanUtils.copyProperties(target, source);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
        return target;
    }
	
	
	public static Centro mapDtoToModel(CentroDto source, Centro target) throws DataIntegrityViolationException {
		//Verificar el id de Hibernate...
		boolean isNew = false;
		if (source.getId() == null) {
			isNew = true;
		}
		//Copiar las propiedades por Reflection
		try {
			BeanUtilsBean.getInstance().getConvertUtils().register(false, false, 0);
			BeanUtils.copyProperties(target, source);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		
		if (isNew) {
			target.setId(null);
		}
		
        return target;
    }
	
}
